var knex = require('../dbhelper').instance();
var promise = require('bluebird');
module.exports = {
    login: function (username, password) {
        return new Promise(function (resolve, reject) {
            knex('users').where({
                    'username': username,
                    'status': 1
                })
                .select('*').then(function (data) {
                    resolve(data);
                }).catch(function (error) {
                    console.error(error);
                    reject(error);
                });
        });
    },
    getlist: function (condition) {
        return new Promise(function (resolve, reject) {
            knex.transaction(function (trx) {
                return Promise.all([
                        knex('users').where(function(){
                            this.where('username', 'like', '%' + condition.key + '%').orWhere('truename', 'like', '%' + condition.key + '%')
                        }).andWhere('status','>=',condition.status).andWhere('createuserid',condition.userid).orWhere('userid',condition.userid)
                        .select('*').limit(condition.limit).offset((condition.page - 1) * condition.limit).orderBy('createat', 'desc').transacting(trx),

                        knex('users').where(function(){
                            this.where('username', 'like', '%' + condition.key + '%').orWhere('truename', 'like', '%' + condition.key + '%')
                        }).andWhere('status','>=',condition.status).andWhere('createuserid',condition.userid).orWhere('userid',condition.userid)
                        .select(knex.raw('count(1) as total')).transacting(trx)
                    ])
                    .then(trx.commit)
                    .catch(trx.rollback);
            }).then(function (resp) {
                resolve(resp);
            }).catch(function (err) {
                console.error(err);
            });
        });
    },
    add: function (params) {
        return new Promise(function (resolve, reject) {
            knex('users').insert(params).then(function (data) {
                resolve(data);
            }).catch(function (error) {
                console.error(error);
                reject(error);
            });
        });
    },
    statuschange: function (params) {
        return new Promise(function (resolve, reject) {
            knex('users').update({
                'status': params.status
            }).where({
                'userid': params.userid
            }).then(function (data) {
                resolve(data);
            }).catch(function (error) {
                console.error(error);
                reject(error);
            });
        });
    },
    resetpassword: function (params) {
        return new Promise(function (resolve, reject) {
            knex('users').update({
                password: params.password,
                salt: params.salt
            }).where({
                'userid': params.userid
            }).then(function (data) {
                resolve(data);
            }).catch(function (error) {
                console.error(error);
                reject(error);
            });
        });
    }
};